package com.sunda.spmsweb.overseacontroller;


import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsoversea.dto.OverseaWhsMoveSearchDTO;
import com.sunda.spmsoversea.dto.OverseaWhsMoveUpdateDTO;
import com.sunda.spmsoversea.service.IOverseaWhsMoveService;
import com.sunda.spmsweb.util.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 海外仓调拨业务 前端控制器
 * </p>
 *
 * @author Wayne
 * @since 2022-01-20
 */
@RestController
@RequestMapping("/overseaWhsMove")
@Api(tags = "海外仓调拨任务", description = "海外仓调拨任务")
public class OverseaWhsMoveController {

    @Autowired
    IOverseaWhsMoveService iOverseaWhsMoveService;

    @RequestMapping("/getOverseaWhsMovePage")
    @ApiOperation(value = "获取调拨任务表头分页", notes = "获取调拨任务表头分页\n" +
            "调拨任务状态(0删除;1保存;2提交SPMS/库存更新;3提交SAP失败;4提交SAP成功;5调拨任务已撤销;)\n" +
            "示例参数：\n" +
            "{\n" +
            "\t\"createDateBegin\": \"\",\n" +
            "\t\"createDateEnd\": \"\",\n" +
            "\t\"createUser\": \"\",\n" +
            "\t\"current\": 1,\n" +
            "\t\"size\": 20,\n" +
            "\t\"spmsStatus\": \"\",\n" +
            "\t\"subject\": \"\",\n" +
            "\t\"werksMoveIn\": \"TF01\",\n" +
            "\t\"werksMoveOut\": \"\",\n" +
            "\t\"whsLocationCodeMoveIn\": \"\",\n" +
            "\t\"whsLocationCodeMoveOut\": \"\",\n" +
            "\t\"whsMoveNo\": \"\"\n" +
            "}", httpMethod = "POST")
    public ResponseResult getOverseaWhsMovePage(@RequestBody OverseaWhsMoveSearchDTO whsMoveSearchDTO){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return ResponseResult.success().add("overseaWhsMoveList", iOverseaWhsMoveService.getOverseaWhsMovePage(whsMoveSearchDTO));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getOverseaWhsMoveDtlList")
    @ApiOperation(value = "获取调拨任务行项目", notes = "获取调拨任务行项目\n" +
            "示例参数：\n" +
            "uuid = d5fba10b3a6e67cee0530101007f630a", httpMethod = "POST")
    public ResponseResult getOverseaWhsMoveDtlList(@RequestParam String uuid){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return ResponseResult.success().add("overseaWhsMoveDtlList", iOverseaWhsMoveService.getOverseaWhsMoveDtlList(uuid));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/updateOverseaWhsMove")
    @ApiOperation(value = "更新或新建海外仓调拨任务", notes = "更新或新建海外仓调拨任务\n" +
            "更新或新建海外仓调拨任务及明细，目标状态：1新建或更新；2提交SPMS更新两边仓库库存；\n" +
            "示例参数：\n" +
            "", httpMethod = "POST")
    public ResponseResult updateOverseaWhsMove(@RequestBody OverseaWhsMoveUpdateDTO overseaWhsMoveUpdateDTO){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaWhsMoveService.updateOverseaWhsMove(overseaWhsMoveUpdateDTO, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/submitOverseaWhsMoveSap")
    @ApiOperation(value = "海外仓调拨任务提交SAP", notes = "海外仓调拨任务提交SAP\n" +
            "调拨任务状态(0删除;1保存;2提交SPMS/库存更新;3提交SAP失败;4提交SAP成功;5调拨任务已撤销;)\n" +
            "海外仓调拨任务提交SAP：2——3、2——4 或 3——4 \n" +
            "示例参数：\n" +
            "", httpMethod = "POST")
    public ResponseResult submitOverseaWhsMoveSap(@RequestParam String uuid,
                                                  @RequestParam String postingDate,
                                                  @RequestParam Integer dataVersion){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaWhsMoveService.submitOverseaWhsMoveSap(uuid, postingDate, dataVersion, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/closeOverseaWhsMoveTask")
    @ApiOperation(value = "海外仓调拨任务关闭", notes = "海外仓调拨任务关闭\n" +
            "调拨任务状态(0删除;1保存;2提交SPMS/库存更新;3提交SAP失败;4提交SAP成功;5调拨任务已撤销;)\n" +
            "海外仓调拨任务关闭 1-0； \n" +
            "示例参数：\n" +
            "", httpMethod = "POST")
    public ResponseResult closeOverseaWhsMoveTask(@RequestParam String uuid,
                                                  @RequestParam Integer dataVersion){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaWhsMoveService.closeOverseaWhsMoveTask(uuid, dataVersion, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/cancelOverseaWhsMove")
    @ApiOperation(value = "海外仓调拨任务撤销", notes = "海外仓调拨任务撤销\n" +
            "调拨任务状态(0删除;1保存;2提交SPMS/库存更新;3提交SAP失败;4提交SAP成功;5调拨任务已撤销;)\n" +
            "海外仓调拨任务撤销，状态变化：2——1，3——1，4——1，都要更新库存； \n" +
            "示例参数：\n" +
            "", httpMethod = "POST")
    public ResponseResult cancelOverseaWhsMove(@RequestParam String uuid,
                                               @RequestParam Integer dataVersion,
                                               String cancelPostingDate){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaWhsMoveService.cancelOverseaWhsMove(uuid, dataVersion, cancelPostingDate, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

}
